      {  Petrova Tatjana
         2012 / 12 / 18
         This program  draws 400 buttons and you can play with them.
         This play  calls 'MINESWIPER'.}
unit Mineswiper;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  intArray=array of integer;
  intArray1=array of TPanel;
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Edit1: TEdit;
    Edit2: TEdit;

    procedure FormCreate(Sender: TObject);
    procedure Panel1Click(Sender: TObject);
    procedure Panel2Click(Sender: TObject);
    procedure Panel1ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  n=5;
  m=4;

var
  Form1: TForm1;
  arr : array [0..(n-1),0..(n-1)] of TPanel;
  arr1 : array of intArray1;
  mass1 : array of intArray;
  mass : array[0..(n-1),0..(n-1)] of integer;


implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i,j,a,b,k : integer;
begin
  for j:=0 to (n-1) do
  begin
    for i := 0 to (n-1) do
    begin
      randomize;
      arr[i][j]:= TPanel.Create(Form1);
      arr[i][j].Width := 20;
      arr[i][j].Height := 20;
      arr[i][j].Parent := Form1;
      arr[i][j].Top := 20*(j mod n);
      arr[i][j].Left := 20*(i mod n);
      arr[i][j].OnClick := Panel1Click;
      arr[i][j].OnContextPopup := Panel1ContextPopup;
   end;
   for a := 0 to (n-1)do
      for b := 0 to (n-1)do
        mass[a][b] :=0;
   for k := 0 to (m-1) do
   begin
        a := random(n-1);
        b := random(n-1);
        mass[a][b] :=1;
   end;
  end;

end;
procedure TForm1.Panel2Click(Sender: TObject);
var i,j,a,b,k,n1,m1 : integer;
begin
  for i:=0 to (n-1) do
    for j := 0 to (n-1) do
    begin
      arr[i][j].Visible:= False;
      arr[i][j].Enabled:= True;
    end;
  n1 :=StrToInt(Edit1.Text);
  m1 := StrToInt(Edit2.Text);
  Setlength(mass1 , n1);
  Setlength(arr1 , n1);
  for i := 0 to  (n1-1) do
  begin
    setLength(mass1[i] ,(n1-1));
    setLength(arr1[i] ,(n1-1));
  end;
  for i:=0 to (n1-1) do
  begin
    for j := 0 to (n1-1) do
    begin
      randomize;
      arr1[i][j]:= TPanel.Create(Form1);
      arr1[i][j].Width := 20;
      arr1[i][j].Height := 20;
      arr1[i][j].Parent := Form1;
      arr1[i][j].Top := 20*(j mod n1);
      arr1[i][j].Left := 20*(i mod n1);
      arr1[i][j].OnClick := Panel1Click;
      arr1[i][j].OnContextPopup := Panel1ContextPopup;
   end;
   for a := 0 to (n1-1)do
      for b := 0 to (n1-1)do
        mass1[a][b] :=0;
   for k := 0 to (m1-1) do
   begin
        a := random(n1-1);
        b := random(n1-1);
        mass1[a][b] :=1;
   end;
  end;
end;
procedure TForm1.Panel1ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
var  i,j: integer;
begin
   for j:=0 to (n-1) do
    for i := 0 to (n-1) do
      if (Sender=arr[i][j]) then
      begin
        if (mass[i][j]=1) then
          if (arr[i][j].Caption <> 'M') then
          begin
              arr[i][j].Color := clRed;
              arr[i][j].Caption := 'M'
          end else
          begin
              arr[i][j].Color := clBtnFace;
              arr[i][j].Caption := '';
          end;
      end;
end;




procedure TForm1.Panel1Click(Sender: TObject);
var i,j,k,d,k1,a,b,c: integer;
begin
  c :=0;
  d :=0;
  for j:=0 to (n-1) do
  begin
    for i := 0 to (n-1) do
    begin
      if (Sender=arr[i][j]) then
      begin
        c :=0;
        a := i;
        b := j;
        if (mass[a][b]=1) then
        begin
          for k:=0 to (n-1) do
            for k1 := 0 to (n-1) do
            begin
              arr[k][k1].Caption := '';
              arr[k][k1].Enabled := False;
              Application.MessageBox('Game over!', 'You have lost!');
              Close;
            end;
        end;
        d := d+1;
        if (d=(n*n-m)) then
        begin
          Application.MessageBox('You have won!', 'If you want to play again click on "New game"');
        end;
        arr[i][j].Enabled := False;
        if((a-1)>=0) and ((b-1)>= 0)and (a<=n) and (b<= n) then
          if (mass[a-1][b-1]=1) then
            c := c+1;
        if((a+1)>=0) and ((b+1)>= 0) and (a<=(n-2)) and (b<=(n-2) ) then
        if (mass[a+1][b+1]=1) then
          c := c+1;
        if((a+1)>=0) and ((b)>= 0) and (a<=(n-2)) and (b<=(n-1))then
          if (mass[a+1][b]=1) then
            c := c+1;
        if((a-1)>=0) and ((b)>=0) and (a<=(n-2)) and (b<=(n-1))then
          if (mass[a-1][b]=1) then
            c := c+1;
        if((a)>=0) and ((b-1)>= 0) and (a<=(n-1)) and (b<=n)then
          if (mass[a][b-1]=1) then
            c := c+1;
        if((a)>=0)and ((b+1)>= 0) and (a<=(n-1)) and (b<=(n-2))then
          if (mass[a][b+1]=1) then
            c := c+1;
        if((a+1)>=0) and ((b-1)>= 0) and (a<=(n-2)) and (b<=n) then
          if (mass[a+1][b-1]=1) then
            c := c+1;
        if((a-1)>=0) and ((b+1)>= 0) and (a<=n) and (b<=(n-2))then
          if (mass[a-1][b+1]=1) then
            c := c+1;
        arr[i][j].Color := clYellow;
        arr[i][j].Caption := IntToStr(c);
      end;
    end;
  end;
end;


end.




